package com.xugongzhang.service;

import com.xugongzhang.dto.Message;
import com.xugongzhang.dto.User;
import com.xugongzhang.enums.MessageTypeEnum;
import com.xugongzhang.thread.ClientConnectServerThread;
import com.xugongzhang.thread.ManageClientConnectServerThread;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author X
 * @date 2022/11/26 16:45
 */
public class UserClientService {
    
    private User user = new User();
    
    private Socket socket;
    
    public boolean checkUser(String username, String password) {
        user.setUserId(username);
        user.setPassword(password);
        boolean flag = false;
        // 连接服务，发送user对象检查
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);
            // 对象输出流
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(user);
            
            // 发送之后读取从服务端收到的message
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            Message msg = (Message) ois.readObject();
            if (MessageTypeEnum.LOGIN_SUCCEED.getMessageType().equals(msg.getMsgType())) {
                // 创建一个和服务器保持通信的线程
                ClientConnectServerThread myThread = new ClientConnectServerThread(socket);
                myThread.start();
                // 客户端可能会有多个线程，所以把他们放在集合中
                ManageClientConnectServerThread.addClientConnectServerThread(username, myThread);
                // 登陆成功，返回true
                flag = true;
            }
            else {
                // 登陆失败，没有启动线程，但是要关闭socket
                socket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
    
    // 向服务器请求在线用户
    public void getOnLineUserList(String username) {
        Message message = new Message();
        message.setMsgType(MessageTypeEnum.GET_ONLINE_FRIEND.getMessageType());
        try {
            ObjectOutputStream oos = new ObjectOutputStream(ManageClientConnectServerThread
                    .getClientConnectServerThread(username)
                    .getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    // 退出
    public void logout() {
        Message message = new Message();
        message.setMsgType(MessageTypeEnum.CLIENT_EXIT.getMessageType());
        message.setSender(user.getUserId());
        try {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(message);
            System.out.println(user.getUserId() + "退出系统");
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
